package cn.gson.springboot.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.gson.springboot.model.dao.UserDao;
import cn.gson.springboot.model.pojos.Section;
import cn.gson.springboot.model.pojos.User;
import cn.gson.springboot.model.service.GrantService;
import cn.gson.springboot.model.service.UserService;
import cn.gson.springboot.model.vo.FunctionVo;
import cn.gson.springboot.model.vo.FunctionVoo;
import cn.gson.springboot.model.vo.Rolevo;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
/*1*/
@Controller
@SessionAttributes({"user"})

public class LandController {
	@Autowired
	private GrantService gs;
	
	@Autowired
	private UserService us;
	
	@Autowired
	private UserDao uda;
	
	  @RequestMapping("demo")
	  public void demo(){
		  
	  }
	
   
		//用户页面
		@RequestMapping("user-role")
		public void user(){
			
		}
	
		
		//查询用户	
		@RequestMapping("user-list")
		@ResponseBody
		public Map<String,Object>  selectAllregistr(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows,String username){
			//进行分页
			 Page<Object> pg = PageHelper.startPage(page, rows);
			 JSONArray userlist = us.findUsers(username);
			 for (Object o : userlist) {				
				}
			//进行数据格式转换和封装
			Map<String,Object> map = new HashMap<>();
			map.put("total", pg.getTotal());//封装总记录数
			map.put("rows", userlist);//封装当前页的内容
			return map;
		}
		
		//新增用户查询科室
		@PostMapping("selcetSection")
		@ResponseBody
		public List<Section> selcetSection() {
			List<Section> kscx = uda.kscx();
			return uda.kscx();
			
		}
		
		
		//新增用户
				@PostMapping("adduser")
				@ResponseBody
				public void adduser(Model m,Integer keshi,String name,String password,String sex,String card,String tel,String Address,String zhuangtai,Integer roleId) {				
				   us.adduser(keshi, name, password, sex, card, tel, Address,"在职", roleId);				  		 
				}
		
		//添加用户角色数据初始化	
		@RequestMapping("userRoleList")
		@ResponseBody
		public List<Rolevo> initGrant(int user_id){
			List<Rolevo> grantList = us.grantList(user_id);
			for (int i = 0; i < grantList.size(); i++) {				
			}
			return us.grantList(user_id);
		}
		
		//添加用户角色
		@PostMapping("adduserrole")
		@ResponseBody
		public void adduser111(Integer userId,String funIdss) {
			List<Integer> ids = new ArrayList<>();
			StringBuffer sb = new StringBuffer(funIdss);
			sb.deleteCharAt(sb.length()-1);
			sb.deleteCharAt(0);
			//切割5,6,7
			for (String s : sb.toString().split(",")) {
				ids.add(Integer.parseInt(s));
			}			
				us.adduser111(userId, ids);
					
		}
	
		
		//新增角色
		@PostMapping("addrole")
		@ResponseBody
		public void addrole(String rolename1) {
			us.addrole(rolename1);			
		}
		//删除角色
		@PostMapping("deleteRole")
		@ResponseBody
		public  void deleteRoleA(Integer roleId) {
			us.deleteRoleA(roleId);
		}
				
		/*修改在职状态*/
		@PostMapping(value="updatestate")
		@ResponseBody
		public void updatestate(Integer userId) {
			User user = us.findUsersbyid(userId);
			 if(user.getUserState().equals("在职")) {
				 us.updatestatre(userId, "离职");
				 us.deleteuserRole(userId);
			 }else if(user.getUserState().equals("离职")) {
				 us.updatestatre(userId, "在职");
			 }
		}
		
		/*修改密码*/
		@PostMapping(value="xg")
		@ResponseBody
		public String updatepwd(Integer userid,String updatpwdq) {			
				us.updatepwd(userid, updatpwdq);
			return "1";
		}
		
		/*查看用户详情*/
		@PostMapping(value="selectAll-userdetails")
		@ResponseBody
		public JSONArray selectAlluserdetails(Integer uid) {
			JSONArray findUsersdetails = us.findUsersdetails(uid);
			return findUsersdetails;
		}
		/*查看用户下的角色*/
		@PostMapping(value="selectAllrole")
		@ResponseBody
		public JSONArray selectAllrole(Integer uid) {
			JSONArray findUsersdetails = us.selectAllrole(uid);
			return findUsersdetails;
		}
		
		//删除用户角色		
		@PostMapping("deleteuser-role")
		@ResponseBody
		public void deleteuser(Integer userId) {
			us.deleteuserRole(userId);
			
		}
		
		/*查看用户的功能*/
		@PostMapping(value="selectAllfunction")
		@ResponseBody
		public List<FunctionVoo> selectAllfunction(Integer user_id) {		
			List<FunctionVoo> grantList = us.selectAllfunction(user_id);	
			for (int i = 0; i < grantList.size(); i++) {				
			}
			return us.selectAllfunction(user_id);	
		}

		
		
		
}
